//
//  ControlTemplateViewDateTimeController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewDateTimeController.h"

#pragma mark -
#pragma mark 日期时间控件

@interface ControlTemplateViewDateTimeController () {
    UILabel* valueLabel; ///显示日期时间值
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;

@end

@implementation ControlTemplateViewDateTimeController

static ControlTemplateViewDateTimeController* sharedManagerDateTimeInter = nil;

+ (ControlTemplateViewDateTimeController*)sharedManagerDateTime
{
    sharedManagerDateTimeInter = [[self alloc] init];
    
    return sharedManagerDateTimeInter;
}

/**
 *  日期时间变量初始化
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [self initVar];
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    
    UILabel* titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [valueLabel setText:[_params valueForKey:@"attrValue"]];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_1];
        [_bgMainView addSubview:valueLabel];
        
        //标题长度换行
        CGSize sizeTitle = STRING_SIZE_FONT(TITLE_WIDTH, [_params valueForKey:@"title"], 16);
        if (sizeTitle.height > TEMPLATE_HEIGHT) {
            //        height = sizeTitle.height;
            titleLabel.frame = CGRectMake(HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - sizeTitle.height) / 2, TITLE_WIDTH, sizeTitle.height);
        }
        else {
            titleLabel.frame = CGRectMake(HEADSPACE, 0, TITLE_WIDTH, TEMPLATE_HEIGHT);
        }
        
    }
    else {
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), TEMPLATE_HEIGHT)];
        abtn.tag = 100;
        abtn.tempData = _params;
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        [abtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgMainView);
        }];
        
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        
        //显示值
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - image.size.width - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_C];
        [valueLabel setText:[_params valueForKey:@"markedWords"]];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        [abtn addSubview:valueLabel];
        
        titleSize.height = titleSize.height > 24 ? titleSize.height : 24;
        valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - image.size.width - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT -24 + titleSize.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(valueLabel.bounds));
        UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
        lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    }
    
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    DateTimePickerViewController* datetimeVC = [[DateTimePickerViewController alloc] init];
    datetimeVC.fundelegate = self;
    datetimeVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    datetimeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        datetimeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    else {
        datetimeVC.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
        //25784 【深到家v1.1.0】IOS 工单无法选择服务分类
    UIViewController *currentVC = [[SEGUhomeMediator sharedInstance] seg_getCurrentController:nil];
    [currentVC presentViewController:datetimeVC animated:YES completion:nil];
}

- (void)onDateTimePickerValue:(NSString*)dateStr
{
    CustomButtonView* abtn = [_bgMainView viewWithTag:100];
    UILabel* aLabel = [abtn.subviews objectAtIndex:0];
    
    if ([_params[@"widgetType"] isEqualToString:@"datetime_nosecond"] && dateStr.length>3) {//不显示秒
        dateStr=[dateStr substringToIndex:dateStr.length-3];
    }
    aLabel.font = kFont_16;
    aLabel.textColor = kColor_1;
    [aLabel setText:dateStr];
    
    _valueStr = dateStr;
    _optional = @"0";
}

/**
 *  给日期时间填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingDateTimeView:(NSString*)valueStr
{
    _valueStr = valueStr;
    if ([_valueStr length] > 0) {
        [valueLabel setText:valueStr];
        [valueLabel setTextColor:kColor_1];
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
}

@end

